package business_logic;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class HighScores
{
    
    LinkedList<Integer> m_highScores = new LinkedList<Integer>();
    int m_scoreListMax;
    
    public HighScores( int maxHighScoresAmmount )
    {
        m_scoreListMax = maxHighScoresAmmount;
    }
    
    public void addScore( int score )
    {
        this.addScore( new Integer(score) );
    }
    
    public void addScore( Integer newScore )
    {
        // Agrego el puntaje a la lista, la ordeno y la trunco al máximo pre-establecido
        m_highScores.add(newScore);
        Collections.sort(m_highScores);
        Collections.reverse(m_highScores);
        
        if( m_highScores.size() > m_scoreListMax )
            m_highScores = new LinkedList<Integer>(m_highScores.subList(0, m_scoreListMax ) );
    }
    
    public void load( String filename )
    {
        try
        {
            m_highScores.clear();
            
            SAXReader reader = new SAXReader();
            
            Document document;
            document = reader.read( filename );
            Element root = document.getRootElement();
            
            // Cargo los HighScores
            @SuppressWarnings("unchecked")
            Iterator<Element> scores = root.elementIterator("Score");
            while( scores.hasNext() )
            {
                Element score = scores.next();
                
                Integer savedScore = Integer.valueOf(score.attributeValue("value"));
                
                m_highScores.add(savedScore);
            }
        
            // Los ordeno y me quedo solo con la cantidad pre-establecida de scores.
            Collections.sort(m_highScores);
            Collections.reverse(m_highScores);
            if( m_highScores.size() > m_scoreListMax )
                m_highScores = new LinkedList<Integer>(m_highScores.subList(0, m_scoreListMax));
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
    }
    
    public void save( String filename )
    {
        Document doc = DocumentHelper.createDocument();
        Element root = DocumentHelper.createElement("HighScores");
        
        for ( Integer score : m_highScores )
        {
            Element scoreNode = DocumentHelper.createElement("Score");
            scoreNode.addAttribute( "value", score.toString() );
            root.add(scoreNode);
        }
        
        doc.add(root);
        
        try
        {
            FileWriter writer;
            writer = new FileWriter(filename);
            doc.write( writer);
            writer.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    public Collection<Integer> getScores()
    {
        return m_highScores;
    }
}
